
package DDE.Dico;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Dico_ArrayList {

private ArrayList <Mot> dico;
	
	public Dico_ArrayList ()  {
		
		//Recuperation du contenu du fichier dans une arraylist
		File input = new File("./src/DDE/Dico/dico.txt");
		Scanner sc = null;
		try {
			sc = new Scanner(input).useDelimiter("\\s*,\\s*|\r\n");
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		dico = new ArrayList<Mot>();
	    while(sc.hasNextLine()) {
	       	Mot mot = new Mot (sc.next(),sc.next());
	       	dico.add(mot);
	    }
	    
	    //Tri alphabethique en Francais
	    Collections.sort(
	    		  dico, 
	    		  new Comparator<Mot>() 
	    		  {
	    		    public int compare(Mot o1, Mot o2) 
	    		    {
	    		      return o1.getMotFr().compareTo(o2.getMotFr());
	    		    }
	    		  }
	    		);
		}
	
	public int getNbElt() {
		int taille = dico.size();
		return taille;
	}
	
	public String traduireToEn(String s){
		String mot = null;
		if(!dico.isEmpty()){
			for (int i=0; i<dico.size();i++){
				if(dico.get(i).getMotFr().equals(s)){
					mot = dico.get(i).getMotEn();
				}
			}
		}
		return mot;
	}
	
	public String traduireToFr(String s){
		String mot = null;
		if(!dico.isEmpty()){
			for (int i=0; i<dico.size();i++){
				if(dico.get(i).getMotEn().equals(s)){
					mot = dico.get(i).getMotFr();
				}
			}
		}
		return mot;
	}

	public String toString(){
		StringBuffer chaine = new StringBuffer();
		for (int i = 0; i < dico.size(); i++){
			chaine = chaine.append(dico.get(i).getMotFr());
		}
		return chaine.toString();
	}
}